package org.scboy.rbac.service.imp;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacResourceUserTypeDao;
import org.scboy.rbac.dao.RbacUserTypeDao;
import org.scboy.rbac.service.UserTypeService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * 用户类型业务类
 * @author yanghao
 *
 */

@Service
public class UserTypeServiceImp implements UserTypeService {

	@Autowired
	EntityFactory factory;
	
	 /**
	   * 分页查询用户类型
	   * @param model 查询参数
	   * @param limit 分页参数
	   * @return
	   */		
	@Override
	public Page page(Model model) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		model.orderBy("sortIndex");
		return entity.page(model);
	}
	
	 /**
	   * 新增用户类型
	   * @param model 用户Model
	   * @return
	   */	
	@Override
	public boolean add(Model model) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		model.add("id", UUID.randomUUID().toString());
		if(model.get("utStatus")==null){
			model.add("utStatus",0);
		}
		
		Model max=entity.maxSortIndex();
		
		Integer sortIndex = max.getInt("sortIndex");
		if (sortIndex == null) {
			sortIndex = 0;
		} else {
			sortIndex++;
		}
		model.add("sortIndex", sortIndex);
		model.add("addTime", new Date());
		model.add("editTime", new Date());
		int resultCount = entity.insert(model);
		if(resultCount> 0){
			return true;
		}				
		return false;
	}
	
	 /**
	   * 更新用户类型
	   * @param model 用户Model
	   * @return
	   */	
	@Override
	public boolean update(Model model) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		model.add("editTime",new Date());
		int resultCount = entity.update(model);
		if(resultCount>0){
			return true;
		}	
		return false;
	}
	
	 /**
	   * 根据用户类型ID获取一个用户类型信息
	   * @param id 用户类型ID
	   * @return
	   */	
	@Override
	public Model get(String id) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		return entity.get(Model.create().add("id", id));
	}
	
	 /**
	   * 删除用户类型
	   * @param id 用户类型ID
	   * @return
	   */	
	@Override
	@Transactional
	public boolean delete(String id) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		RbacResourceUserTypeDao utDao=factory.getDao(RbacResourceUserTypeDao.class);
		utDao.delete(Model.create().add("userTypeId", id));
		int resultCount = entity.delete(Model.create().add("id", id));
		if(resultCount>0){
			return true;
		}	
		return false;
		
	}

	  /**
	   * 启用禁用
	   * @param id 用户类型ID
	   * @return
	   */
	@Override
	public boolean checkState(String id) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		Model model = get(id);
		if(model==null){
		  return false;
		}
		 model.add("utStatus", !model.getBoolean("utStatus"));
		 int resultCount =entity.update(model);
		 if(resultCount>0){
				return true;
			}else{
				return false;
			}
	}
	 /**
	   * 获取启用的用户类型
	   * @param model
	   * @return
	   */
	@Override
	public List<Model> list(Model model) {
		RbacUserTypeDao entity=factory.getDao(RbacUserTypeDao.class);
		model.add("utStatus", true);
	    return entity.select(model);
	}
	  

	@Override
	public boolean sort(String id, int type) {
		RbacUserTypeDao dao=factory.getDao(RbacUserTypeDao.class);
		Model model=dao.get(Model.create().add("id", id));
		Model lastModel=null;
		String sortKey="sortIndex";
		String idKey="id";
		//降序
		if(type==0) {
			//查询下面一个系统
			lastModel=dao.get(Model.create().GT(sortKey, model.getInt(sortKey)).orderBy(sortKey));
			
		}else {
			//升序
			lastModel=dao.get(Model.create().LT(sortKey, model.getInt(sortKey)).orderBy(sortKey,OrderType.DESC));
			
		}
		if(lastModel!=null) {
			dao.update(Model.create().add(sortKey, lastModel.getInt(sortKey)),Model.create().add(idKey, id));
			dao.update(Model.create().add(sortKey, model.getInt(sortKey)),Model.create().add(idKey, lastModel.getString(idKey)));
		}
		return true;
	}
	

}
